<?php
namespace app\admin\controller;

use \think\Controller;
use \think\Db;
class Apilogin extends Common
{


    /**
     * 账号密码登录
     * @return bool
     * 参数：账号、密码
     * 返回值：0参数错误，1手机号码错误，2密码格式错误，3没有此用户，4密码错误，5成功
     */
    public  function userLogin(){

        header("Access-Control-Allow-Origin:*");
        if(!isset($_POST['phone']) || !isset($_POST['pass'])){
            return json_encode(['status'=>0,'msg'=>'参数错误']);
        }
        $phone = $_POST['phone'];
        $pass = $_POST['pass'];
        if(preg_match("/^1[3456789]\d{9}$/", $phone)){
            if(empty($pass) || !isset($pass)){
                return json_encode(['status'=>2,'msg'=>'请填写正确的密码']);
            }else{
                $username = Db::table('up_u_user')->where('user_phone',$phone)->find();
                if(empty($username)){
                    return json_encode(['status'=>3,'msg'=>'没有此用户']);
                }else{
                    if($username['user_password'] != md5($pass)){
                        return json_encode(['status'=>4,'msg'=>'密码错误']);
                    }else{
                        //更新token
                        $token = md5(date('YmdHis') . rand(100000,999999));
                        Db::table('up_u_user')->where('user_phone',$phone)
                            ->update(['user_token'=>$token,'equipment'=>$_POST['equipment']]);
                        $rytoken = $this->rongyun($username['user_id'],$username['user_username'],$username['user_image']);
                        $username = Db::table('up_u_user')->where('user_phone',$phone)->find();
                        //绑定失败的话提示报错信息
                        if(!isset($rytoken['token'])){
                            return json_encode(['status'=>5,'msg'=>$rytoken['errorMessage']]);
                        }
                        //--------end----
                        $username['rytoken'] = $rytoken['token'];
                        return json_encode(['status'=>200,'msg'=>$username]);
                    }
                }
            }
        }else{
            return json_encode(['status'=>1,'msg'=>'请填写正确的手机号码']);
        }
    }

    /**
     * 手机登录或注册时发送的验证码
     * @return bool
     * 参数：phone type(1是登录和更换手机号(第一步) 2是注册和更换手机号(第二步) 3是忘记密码)
     * 返回值：0参数错误，1手机格式错误，21登录无此号，22注册有此号，3验证码发送失败，4返回md5加密验证码
     */
    public  function selectCodeDx(){//echo 1;die;
//        header("Access-Control-Allow-Origin:*");
        if(!isset($_POST['phone']) || !isset($_POST['type'])){
            return json_encode(['status'=>0,'msg'=>'参数错误']);
        }
        $num = rand(100000,999999);
        $phone = $_POST['phone'];
        $type = $_POST['type'];
        if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
            return json_encode(['status'=>1,'msg'=>'请填写正确的手机号码']);
        }
        $username = Db::table('up_u_user')->where('user_phone',$phone)->find();
        if(empty($username) && $type==1){
            return json_encode(['status'=>21,'msg'=>'没有此手机号码用户']);
        }
        if(empty($username) && $type==3){
            return json_encode(['status'=>21,'msg'=>'没有此手机号码用户']);
        }
        if(!empty($username) && $type==2){
            return json_encode(['status'=>22,'msg'=>'已经有此手机号码用户']);
        }
        //验证码判断
        if($type == 2){
            $yzm  = input('param.yzm');
            $yzm_data = Db::table('up_phonecode')->where('phone',$phone)->find();
            if($yzm_data['yzm'] != $yzm){
                return json_encode(['status'=>22,'msg'=>'验证码错误']);
            }
            if($yzm_data['yzm_status'] != 1){
                return json_encode(['status'=>22,'msg'=>'此验证码已使用，请重新获取']);
            }
            Db::table('up_phonecode')->where('phone',$phone)->update(['yzm_status'=>2]);
        }
        $sendres = $this->duaxin($phone,$num);
        if($sendres['code'] != '00000'){
            return json_encode(['status'=>3,'msg'=>$sendres['msg']]);
        }else {
            //=============================
            $isset = Db::table('up_phonecode')->where('phone', $phone)->find();
            if ($isset) {
                $res = Db::table('up_phonecode')->where('phone', $phone)->update(['code' => md5($num), 'time2' => time(), 'status' => 0]);
            } else {
                $res = Db::table('up_phonecode')->insert(['phone' => $phone, 'code' => md5($num), 'time2' => time()]);
            }
            if (!$res) {
                return json_encode(['status' => 31, 'msg' => '数据库操作失败']);
            }
            //===============================================
            return json_encode(['status' => 200, 'msg' => md5($num)]);
        }
    }
    /**
     * 手机号码登录(暂无)
     * @return bool
     * 参数：phone，code
     * 返回：0参数错误，1手机格式错误，21验证码错误，22验证码超时，3无此用户，4成功
     */

//    public  function phoneLogin(){
//        header("Access-Control-Allow-Origin:*");
//        if(!isset($_POST['phone']) || !isset($_POST['code'])){
//            return json_encode(['status'=>0,'msg'=>'参数错误']);
//        }
//        $phone = $_POST['phone'];
//        $code = $_POST['code'];
//
//        if(!preg_match("/^1[34578]\d{9}$/", $phone)){
//            return json_encode(['status'=>1,'msg'=>'请填写正确的手机号码']);
//        }
//        //=============================================
//        $res = Db::table('up_phonecode')->where('phone',$phone)->find();
//        //=============================================================
//        if($code != $res['code']){
//            return json_encode(['status'=>21,'msg'=>'验证码错误']);
//        }
//        $now = time();
//        if($now - $res['time2'] >= 601){
//            return json_encode(['status'=>22,'msg'=>'验证码超时']);
//        }
//        $username = Db::table('ew_appuser')->alias('a')
//            ->join('ew_subject s','a.majorid = s.s_id','LEFT')
//            ->where('username',$phone)->find();
//        if(empty($username)){
//            return json_encode(['status'=>3,'msg'=>'没有此手机号码用户']);
//        }
////        $username = Db::table('ew_appuser')
////            ->where('username',$phone)
////            ->find();
//        return json_encode(['status'=>200,'msg'=>$username]);
//    }

    /**
     * 注册
     * @return bool
     * 参数：phone,code,name,password,major
     * 返回：0参数错误，1手机格式错误，21验证码错误，22验证码超时，3无此用户，4有其他未填项，5插入登录表失败，200成功
     */

    public  function userReg(){
        header("Access-Control-Allow-Origin:*");
        if(!isset($_POST['phone']) || !isset($_POST['code'])){
            return json_encode(['status'=>0,'msg'=>'参数错误']);
        }
        if(!isset($_POST['password'])){
            return json_encode(['status'=>0,'msg'=>'参数错误']);
        }
        $phone = $_POST['phone'];
        $code = $_POST['code'];
        $pass = $_POST['password'];
        if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
            return json_encode(['status'=>1,'msg'=>'请填写正确的手机号码']);
        }
        $username = Db::table('up_u_user')->where('user_phone',$phone)->find();
        if(!empty($username)){
            return json_encode(['status'=>3,'msg'=>'已经有此手机号码用户']);
        }
        if($pass=='' || $code==''){
            return json_encode(['status'=>4,'msg'=>'有未填项，请检查']);
        }
        //=============================================
        $res = Db::table('up_phonecode')->where('phone',$phone)->find();
        //=============================================================
        if($code != $res['code']){
            return json_encode(['status'=>21,'msg'=>'验证码错误']);
        }
        $now = time();
        if($now - $res['time2'] >= 6501){
            return json_encode(['status'=>22,'msg'=>'验证码超时']);
        }
        if($res['status']==1){
            return json_encode(['status'=>23,'msg'=>'验证码已使用']);
        }
        $token = md5(date('YmdHis').rand(100000,999999));
        $data = array();
        $data['user_password'] = md5($pass);
        $data['user_time'] = date('Y-m-d H:i:s');
        $data['user_register_time'] = date('Y-m-d');
        $data['user_year'] = date('Y');
        $data['user_month'] = date('Y-m');
        $data['user_token'] = $token;
        $data['user_phone'] = $phone;
        $data['user_attestation'] = 1;//未认证
        $data['user_username'] = '用户' . rand(100000,999999);//未认证
        $data['equipment'] = $_POST['equipment'];//设备号
        $id = Db::name('u_user')->insertGetId($data);
        if($id){
            $user = Db::table('up_u_user')
                ->where('user_id',$id)
                ->find();
            Db::table('up_phonecode')->where('phone',$phone)->update(['status'=>1]);
            //注册完成积分+10
            Db::table('up_integral')
                ->insert(['integral_status'=>1,'integral_user_id'=>$id,'integral_time'=>date('Y-m-d H:i:s'),'integral_num'=>10]);
            Db::table('up_u_user')
                ->where('user_id',$id)
                ->setInc('user_integral',10);
            $rytoken = $this->rongyun($user['user_id'],$user['user_username'],$user['user_image']);
            $user['rytoken'] = $rytoken['token'];
            return json_encode(['status'=>200,'msg'=>$user]);
        }else{
            return json_encode(['status'=>5,'msg'=>'生成登录数据失败']);
        }
    }

    /**
     * 忘记密码第一步：验证码
     * @return bool
     * 参数：phone,code
     * 返回：0参数错误，1手机格式错误，21验证码错误，22验证码超时，3无此用户，200成功
     */

    public  function forgetPass(){
        header("Access-Control-Allow-Origin:*");
        if(!isset($_POST['phone']) || !isset($_POST['code'])){
            return json_encode(['status'=>0,'msg'=>'参数错误']);
        }
        $phone = $_POST['phone'];
        $code = $_POST['code'];
        $new = $_POST['newpass'];
        if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
            return json_encode(['status'=>1,'msg'=>'请填写正确的手机号码']);
        }
        if($new == '' || $code == ''){
            return json_encode(['status'=>2,'msg'=>'有未填项']);
        }
        $username = Db::table('up_u_user')->where('user_phone',$phone)->find();
        if(empty($username)){
            return json_encode(['status'=>3,'msg'=>'没有此手机号码用户']);
        }
        //=============================================
        $res = Db::table('up_phonecode')->where('phone',$phone)->find();
        //=============================================================
        if($code != $res['code']){
            return json_encode(['status'=>21,'msg'=>'验证码错误']);
        }
        $now = time();
        if($now - $res['time2'] >= 601){
            return json_encode(['status'=>22,'msg'=>'验证码超时']);
        }
        if($res['status']==1){
            return json_encode(['status'=>23,'msg'=>'验证码已使用']);
        }
        //TODO 修改密码成功后如果直接登录那么就要更新token
        $token = md5(date('YmdHis').rand(100000,999999));
        //,'user_token'=>$token
        $res = Db::table('up_u_user')->where('user_phone',$phone)->update(['user_password'=>md5($new),'user_token'=>$token]);
        Db::table('up_phonecode')->where('phone',$phone)->update(['status'=>1]);
        if($res){
            $user = Db::table('up_u_user')->where('user_phone',$phone)->find();
            $rytoken = $this->rongyun($user['user_id'],$user['user_username'],$user['user_image']);
            $user['rytoken'] = $rytoken['token'];
            return json_encode(['status'=>200,'msg'=>$user]);
        }else{
            return json_encode(['status'=>3,'msg'=>'密码重置失败']);
        }
    }










    /*=====搜索Start=====*/
    /**
     * searchUser 搜索用户数据
     * @$page 分页
     * @param $keyword 搜索关键字
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return array 返回数组
     */
    public function searchUser(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $whereUser['user_username'] = ['like',"%$keyword%"];
        $user = Db::table('up_u_user')
            ->where($whereUser)
            ->field('user_id,user_username,user_image,user_integral,user_attestation')
            ->limit(($page-1)*10,10)
            ->select();
        return json_encode(['status'=>200,'msg'=>$user]);
    }

    /**
     * searchDog 搜索狗数据
     * @$page 分页
     * @param $keyword 搜索关键字
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return array 返回数组
     */
    public function searchDog(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $whereDog['udf_name'] = ['like',"%$keyword%"];
        $dog = Db::table('up_dog_family')
            ->where($whereDog)
            ->field('udf_id,udf_name,udf_sex,udf_age,udf_img')
            ->limit(($page-1)*10,10)
            ->select();
        for($i=0;$i<count($dog);$i++){
            switch ($dog[$i]['udf_age'])
            {
                case 1:
                    $dog[$i]['udf_age'] = "不满12个月";
                    break;
                case 2:
                    $dog[$i]['udf_age'] = "1-3岁";
                    break;
                case 3:
                    $dog[$i]['udf_age'] = "4-6岁";
                    break;
                case 4:
                    $dog[$i]['udf_age'] = "7岁以上";
                    break;
                default:
                    $dog[$i]['udf_age'] = "不详";
            }
            switch ($dog[$i]['udf_sex'])
            {
                case 1:
                    $dog[$i]['udf_sex'] = "公";
                    break;
                default:
                    $dog[$i]['udf_sex'] = "母";
            }
        }
        return json_encode(['status'=>200,'msg'=>$dog]);
    }
    /*=====搜索End=====*/

    /*=====首页Start=====*/
    /**
     * getHomeData  首页数据
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param $page 页码
     * @param $shi 市
     * @return string
     */
    public function getHomeData(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        Db::table('up_u_user')->where('user_id',$user_id)->update(['user_login_time'=>date('Y-m-d')]);
        if($shi !== ""){
            $where1 = [
                'ud.dog_preserve'=>2,
                'ud.area_shi'=>$shi
            ];
            $data = $this->getHomePageData($where1,$page);
            return json_encode(['status'=>200,'msg'=>$data]);
        }else{
            $where2 = [
                'dog_preserve'=>2
            ];
            $data = $this->getHomePageData($where2,$page);
            return json_encode(['status'=>200,'msg'=>$data]);
        }
    }
    /**
     * getHomePageData 获取首页数据
     * @return 返回数组
     */
    public function getHomePageData($where,$page){
        if($page>=10){
            $page = 10;
        }
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_area u','ud.area_sheng=u.district_id','left')
            ->join('up_area ua','ud.area_shi=ua.district_id','left')
            ->join('up_area uaa','ud.area_qu=uaa.district_id','left')
            ->join('up_dog_family udf','ud.udf_id=udf.udf_id','left')
            ->join('up_dog_seek_characteristic uds','ud.dog_seek_characteristic=uds.udskc_id','left')
            ->where($where)
            ->where (function ($query){
                $query->where( 'ud.dog_status',"IN",[1,2,3])->whereOr( 'ud.dog_help_status',1)->whereOr('ud.dog_seek_status',1)->whereOr('ud.dog_adopt_status',2);//只能看到2
            })
            ->field('ud.dog_type,ud.dog_id,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,
ua.district shi,uaa.district qu,ud.dog_num,ud.udsc_id_information,ud.dog_status,ud.dog_img,ud.udsc_id,ud.dog_release_time,ud.dog_add,udf.udf_name,ud.dog_help_status,ud.dog_information,ud.dog_end_time,ud.dog_help_information,ud.dog_help_style,u.district shen,ud.dog_adopt_status,udf.udf_sex,udf.udf_age,udf.udf_dog_sterilization,udf.udf_dog_outside,udf.udf_dog_inside,udf.udf_dog_vaccine,udf.udf_dog_disability,udf.udf_img,uuu.user_username,uuu.user_image,uuu.user_attestation,ud.dog_seek_time,ud.dog_seek_status,uds.udskc_habitus,uds.udskc_hair,uds.udskc_sex,uds.udskc_tail,uds.udskca_id,uds.udskca_id_other,uds.udskci_id,uds.udskci_id_other')
            ->order('ud.dog_release_time desc')
            ->limit(($page-1)*10,10)
            ->select();
        for($i = 0;$i<count($data);$i++){
            if($data[$i]['dog_type'] == 1){
                $a = trim($data[$i]['udsc_id'],',');
                $b = Db::table('up_dog_strat_characteristic')
                    ->where('udsc_id','IN',$a)
                    ->column('udsc_name');
                $num = explode(',',$a);
                if(in_array(0,$num)){
                    $b['other'] = $data[$i]['udsc_id_information'];
                }
                $data[$i]['characteristic'] = $b;
            }elseif($data[$i]['dog_type'] == 2){
                $a = trim($data[$i]['dog_help_style'],',');
                $b = Db::table('up_dog_heo')
                    ->where('heo_id','IN',$a)
                    ->column('heo_name');
                $num = explode(',',$a);
                if(in_array(0,$num)){
                    $b['other'] = $data[$i]['dog_help_information'];
                }
                $data[$i]['heo_category'] = $b;
            }elseif($data[$i]['dog_type'] == 4){
                $b = Db::table('up_dog_seek_characteristic_apparel')
                    ->where('udskca_id','IN',$data[$i]['udskca_id'])
                    ->column('udskca_name');
                $a = Db::table('up_dog_seek_characteristic_other')
                    ->where('udsci_id','IN',$data[$i]['udskci_id'])
                    ->column('udsci_name');
                $b_num = explode(',',$data[$i]['udskca_id']);
                $a_num = explode(',',$data[$i]['udskci_id']);
                if(in_array(0,$b_num)){
                    $b['other'] = $data[$i]['udskca_id_other'];
                }
                if(in_array(0,$a_num)){
                    $a['other'] = $data[$i]['udskci_id_other'];
                }

                $data[$i]['apparel'] = $b;
                $data[$i]['others'] = $a;
            }
        }
        return $data;
    }
    /*=====首页End=====*/

    /*=====流浪狗Start=====*/
    /**
     * getStrayDogCharacteristic 获取流浪狗特征&区域
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param $shi 市
     * @return 返回数组
     */
    public function getStrayDogCharacteristic(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $char = Db::table('up_dog_strat_characteristic')
            ->select();
        $place = Db::table('up_area')->where(['pid'=>$shi])->select();
        $data = [
            'Characteristic'=>$char,
            'place'=>$place
        ];
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     *headerStyle 获取流浪狗展示数据
     * @param 1.$shi市
     * @param $type类型(1代表"距离最近"、2代表"最近发布")
     * @param $qu区
     * @param $charact流浪狗特征
     * @param $lon经度
     * @param $lat纬度
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param  $page 页码
     * @return 返回数组
     */
    public function headerStyle(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        /*区*/
        if($qu !== ""){
            $where['ud.area_qu'] = $qu;
        }
        /*流浪狗*/
        $where['ud.dog_type'] = 1;
        /*用户指定的市*/
        $where['ud.area_shi'] = $shi;
        /*已发布的*/
        $where['ud.dog_preserve'] = 2;
        /*默认最近发布的*/
        $sort = "ud.dog_release_time desc";
        $field = 'ud.dog_id,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,u.district sheng,ua.district shi,uaa.district qu,ud.dog_num,ud.udsc_id_information,ud.dog_information,ud.dog_status,ud.dog_img,ud.udsc_id,ud.dog_release_time,ud.dog_id,ud.dog_add';
        if($type == 1 && $lon!=="" && $lat!==""){
            $field = 'ud.dog_id,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,u.district sheng,ua.district shi,uaa.district qu,ud.dog_num,ud.udsc_id_information,ud.dog_information,ud.dog_status,ud.dog_img,ud.udsc_id,ud.dog_release_time,ud.dog_id,ud.dog_add,ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(('.$lat.'* PI() / 180 - ud.dog_lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(ud.dog_lat * PI() / 180) * POW(SIN(('.$lon.' * PI() / 180 - ud.dog_lon * PI() / 180) / 2),2))) * 1000) AS distance';
            $sort = "distance asc";
        }
        /*前台获取的特征数据*/
        if($charact !== ""){
            $data = explode(',',$charact);
            if(count($data) > 1){
                foreach($data  as $vo){
                    $we[] = ['like',"%,$vo,%"];
                }
                $where['ud.udsc_id'] = [$we,'OR'];
            }else{
                $where['ud.udsc_id'] = ['like','%,'.$data[0].',%'];
            }
            $arr =  $this->getStrayDogData($where,$sort,$field,$page);
        }else{
            $arr =  $this->getStrayDogData($where,$sort,$field,$page);
        }
        for($i=0;$i<count($arr);$i++){
            switch ($arr[$i]['dog_status'])
            {
                case 1:
                    $arr[$i]['dog_status'] = "待救助";
                    break;
                case 2:
                    $arr[$i]['dog_status'] = "已救助";
                    break;
                default:
                    $arr[$i]['dog_status'] = "失踪";
            }
        }
        return json_encode(['status'=>200,'msg'=>$arr]);
    }

    /**
     *getStrayDogData  获取流浪狗信息
     * @param  $where 用户对应的市、用户对应的区
     * @param  $sort筛选数据方式 发布时间(ud.dog_update_time desc)和距我最近(distance asc)（默认为发布时间）
     * @param  $field 查询字段
     * @param  $page 页码
     */
    protected function getStrayDogData($where,$sort,$field,$page)
    {
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_area u','ud.area_sheng=u.district_id','left')
            ->join('up_area ua','ud.area_shi=ua.district_id','left')
            ->join('up_area uaa','ud.area_qu=uaa.district_id','left')
            ->where($where)
            ->field($field)
            ->order($sort)
            ->limit(($page-1)*10,10)
            ->select();
        for($i = 0;$i<count($data);$i++){
            $a = trim($data[$i]['udsc_id'],',');
            $b = Db::table('up_dog_strat_characteristic')
                ->where('udsc_id','IN',$a)
                ->column('udsc_name');
            $num = explode(',',$a);
            if(in_array(0,$num)){
                $b['other'] = $data[$i]['udsc_id_information'];
            }
            $data[$i]['characteristic'] = $b;
        }
        return $data;
    }

    /**
     *getStrayDogDetail  单条流浪狗记录详情
     * @param  $dog_id 流浪狗的id
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数组
     */
    public function getStrayDogDetail(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_area u','ud.area_sheng=u.district_id','left')
            ->join('up_area ua','ud.area_shi=ua.district_id','left')
            ->join('up_area uaa','ud.area_qu=uaa.district_id','left')
            ->where(['ud.dog_id'=>$dog_id,'ud.dog_type'=>1,'ud.dog_preserve'=>2])
            ->field('uuu.user_id,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,ud.dog_release_time,ud.dog_img,ud.dog_num,ud.dog_attention_num,ud.dog_status,ud.udsc_id,ud.dog_information,ud.udsc_id_information,ud.dog_add,u.district shen,ua.district shi,uaa.district qu,ud.dog_lat,ud.dog_lon')
            ->find();
        $a = trim($data['udsc_id'],',');
        $b = Db::table('up_dog_strat_characteristic')
            ->where('udsc_id','IN',$a)
            ->column('udsc_name');
        $num = explode(',',$a);
        if(in_array(0,$num)){
            $b['other'] = $data['udsc_id_information'];
        }
        $data['characteristic'] = $b;
        switch ($data['dog_status'])
        {
            case 1:
                $data['dog_status'] = "待救助";
                break;
            case 2:
                $data['dog_status'] = "已救助";
                break;
            default:
                $data['dog_status'] = "失踪";
        }
        if($data['dog_img']){
            $img = trim($data['dog_img'],',');
            $data['dog_img'] = explode(',',$img);
        }

        $res = Db::table('up_u_user_interest_dynamic')
            ->where(['user_id'=>$user_id,'dog_id'=>$dog_id])
            ->find();
        if($res){
            $data['attention'] = 1;
        }else{
            $data['attention'] = 2;
        }
        Db::table("up_dog")->where(['dog_id'=>$dog_id])->setInc ("dog_click_num");
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /*=====流浪狗End=====*/


    /*=====互助Start=====*/


    /**
     * getCategory 获取互助类别
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数组
     */
    public function getCategory(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_dog_heo')
            ->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     *otherData 获取互助展示数据
     * @param $shi市
     * @param $type类型(1代表"距离最近"、2代表"最近发布")
     * @param $qu区
     * @param $category互助类别
     * @param $lon经度
     * @param $lat纬度
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param $page 页码
     * @return 返回数组
     */
    public function otherData(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        /*区*/
        if($qu != ''){
            $where['ud.area_qu'] = $qu;
        }
        /*互助*/
        $where['ud.dog_type'] = 2;
        /*用户指定的市*/
        $where['ud.area_shi'] = $shi;
        /*已发布的且未过期的*/
        $where['ud.dog_preserve'] = 2;
        $where['ud.dog_help_status'] = 1;
        $where['ud.dog_end_time'] = array('>=',date('Y-m-d H:i:s'));
        /*默认最近发布的*/
        $sort = "ud.dog_release_time desc";

        $field = 'ud.dog_id,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,uuu.user_integral,uuu.user_attestation,ua.district shi,uaa.district qu,udf.udf_name,udf.udf_img,ud.dog_help_status,ud.dog_information,ud.dog_end_time,ud.dog_help_information,ud.dog_release_time,ud.dog_help_style,ud.dog_img';
        if($type == 1 && $lon!=="" && $lat!==""){
            $field = 'ud.dog_id,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,uuu.user_integral,uuu.user_attestation,ua.district shi,uaa.district qu,udf.udf_name,udf.udf_img,ud.dog_help_status,ud.dog_information,ud.dog_end_time,ud.dog_help_information,ud.dog_release_time,ud.dog_help_style,ud.dog_img,ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(('.$lat.'* PI() / 180 - ud.dog_lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(ud.dog_lat * PI() / 180) * POW(SIN(('.$lon.' * PI() / 180 - ud.dog_lon * PI() / 180) / 2),2))) * 1000) AS distance';
            $sort = "distance asc";
        }
        /*前台获取的特征数据*/
        if($category !== ""){
            $data = explode(',',$category);
            if(count($data) > 1){
                foreach($data  as $vo){
                    $we[] = ['like',"%,$vo,%"];
                }
                $where['ud.dog_help_style'] = [$we,'OR'];
            }else{
                $where['ud.dog_help_style'] = ['like','%,'.$data[0].',%'];
            }
            $arr =  $this->getHelpEachOtherData($where,$sort,$field,$page);
        }else{
            $arr =  $this->getHelpEachOtherData($where,$sort,$field,$page);
        }
        for($i=0;$i<count($arr);$i++){
            switch($arr[$i]['dog_help_status'])
            {
                case 1:
                    $arr[$i]['dog_help_status'] = "待帮助";
                    break;
                case 2:
                    $arr[$i]['dog_help_status'] = "已解决";
                    break;
                case 3:
                    $arr[$i]['dog_help_status'] = "暂停帮助";
                    break;
                default:
                    $arr[$i]['dog_help_status'] = "已过期";
            }
        }
        return json_encode(['status'=>200,'msg'=>$arr]);
    }



    /**
     *getHelpEachOtherData  获取互助信息
     * @param  $where 用户对应的市
     * @param  $sort筛选数据方式 发布时间(ud.dog_update_time desc)和距我最近(distance asc)（默认为发布时间）
     * @param  $field 查询字段
     * @param  $page 页码
     */
    public function getHelpEachOtherData($where,$sort,$field,$page){
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_area ua','ud.area_shi=ua.district_id','left')
            ->join('up_area uaa','ud.area_qu=uaa.district_id','left')
            ->join('up_dog_family udf','ud.udf_id=udf.udf_id','left')
            ->where($where)
            ->field($field)
            ->order($sort)
            ->limit(($page-1)*10,10)
            ->select();
        for($i = 0;$i<count($data);$i++){
            $a = trim($data[$i]['dog_help_style'],',');
            $b = Db::table('up_dog_heo')
                ->where('heo_id','IN',$a)
                ->column('heo_name');
            $num = explode(',',$a);
            if(in_array(0,$num)){
                $b['other'] = $data[$i]['dog_help_information'];
            }
            $data[$i]['heo_category'] = $b;
        }
        return $data;
    }

    /**
     * getHelpEachOtherDetail  单条互助记录详情
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param  $dog_id 互助狗的id
     */
    public function getHelpEachOtherDetail(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_dog_family udf','ud.udf_id=udf.udf_id','left')
            ->where(['ud.dog_id'=>$dog_id,'ud.dog_type'=>2,'ud.dog_preserve'=>2])
            ->field('uuu.user_id,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,ud.dog_help_status,udf.udf_name,udf.udf_img,ud.dog_information,ud.dog_end_time,ud.dog_help_information,ud.dog_release_time,ud.dog_help_style,ud.dog_img,ud.dog_attention_num,ud.dog_id,udf.udf_id')
            ->find();
        if($data['dog_img']){
            $img = trim($data['dog_img'],',');
            $data['dog_img'] = explode(',',$img);
        }
		//else{
        //    if($data['udf_img']){
        //        $img = trim($data['udf_img'],',');
        //        $data['dog_img'] = explode(',',$img);
        //    }
        //}
        $a = trim($data['dog_help_style'],',');
        $b = Db::table('up_dog_heo')
            ->where('heo_id','IN',$a)
            ->column('heo_name');
        $num = explode(',',$a);
        if(in_array(0,$num)){
            $b['other'] = $data['dog_help_information'];
        }
        $data['heo_category'] = $b;
        switch($data['dog_help_status'])
        {
            case 1:
                $data['dog_help_status'] = "待帮助";
                break;
            case 2:
                $data['dog_help_status'] = "已解决";
                break;
            case 3:
                $data['dog_help_status'] = "暂停帮助";
                break;
            default:
                $data['dog_help_status'] = "已过期";
        }
        $res = Db::table('up_u_user_interest_dynamic')
            ->where(['user_id'=>$user_id,'dog_id'=>$dog_id])
            ->find();
        if($res){
            $data['attention'] = 1;
        }else{
            $data['attention'] = 2;
        }
        Db::table("up_dog")->where(['dog_id'=>$dog_id])->setInc ("dog_click_num");
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /*=====互助End=====*/


    /*=====领养Start=====*/
    /**
     * adopt 获取领养展示数据
     * @param $shi 市
     * @param $qu 区
     * @param $age 年龄
     * @param $sex 性别
     * @param $ste 是否绝育
     * @param $type 是否有照片
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param $page 页码
     * @return 返回数组
     */
    public function adopt(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $where = array();
        /*领养*/
        $where['ud.dog_type'] = 3;
        /*用户指定的市*/
        $where['ud.area_shi'] = $shi;
        /*已发布的*/
        $where['ud.dog_preserve'] = 2;
        /*年龄*/
        if($age !== ""){
            $where['udf.udf_age'] = $age;
        }
        /*性别*/
        if($sex !== ""){
            $where['udf.udf_sex'] = $sex;
        }
        /*绝育*/
        if($ste !== ""){
            $where['udf_dog_sterilization'] = $ste;
        }
        /*照片*/
        if($type ==1){
            /*有照片*/
            $where['udf.udf_img'] = ['<>'," "];
        }elseif($type ==2){
            /*无照片*/
            $where['udf.udf_img'] = ['='," "];
        }

        /*2：待领养   4：重新领养*/
//        $where['ud.dog_adopt_status'] = ['in',"2,4"];
        $where['ud.dog_adopt_status'] = 2;
        $arr = $this->getAdoptData($where,$page);
        for($i=0;$i<count($arr);$i++){
            switch($arr[$i]['dog_adopt_status'])
            {
                case 1:
                    $arr[$i]['dog_adopt_status'] = "取消领养";
                    break;
                case 2:
                    $arr[$i]['dog_adopt_status'] = "待领养";
                    break;
                case 3:
                    $arr[$i]['dog_adopt_status'] = "完成领养";
                    break;
                default:
                    $arr[$i]['dog_adopt_status'] = "重新领养";
            }
            switch($arr[$i]['udf_sex'])
            {
                case 1:
                    $arr[$i]['udf_sex'] = "公";
                    break;
                default:
                    $arr[$i]['udf_sex'] = "母";
            }
            switch($arr[$i]['udf_age'])
            {
                case 1:
                    $arr[$i]['udf_age'] = "不满12个月";
                    break;
                case 2:
                    $arr[$i]['udf_age'] = "1-3岁";
                    break;
                case 3:
                    $arr[$i]['udf_age'] = "4-6岁";
                    break;
                case 4:
                    $arr[$i]['udf_age'] = "7岁以上";
                    break;
                default:
                    $arr[$i]['udf_age'] = "年龄不详";
            }
        }
        return json_encode(['status'=>200,'msg'=>$arr]);
    }

    /**
     *getAdoptData  领养狗记录数据
     * @param  $where 领养狗搜索条件
     * @param $page 页码
     */
    public function getAdoptData($where,$page){
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_area u','ud.area_sheng=u.district_id','left')
            ->join('up_area ua','ud.area_shi=ua.district_id','left')
            ->join('up_area uaa','ud.area_qu=uaa.district_id','left')
            ->join('up_dog_family udf','ud.udf_id=udf.udf_id','left')
            ->where($where)
            ->field('ud.dog_id,ud.dog_release_time,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,u.district shen,ua.district shi,uaa.district qu,ud.dog_adopt_status,udf.udf_name,udf.udf_sex,udf.udf_age,udf.udf_dog_sterilization,udf.udf_dog_outside,udf.udf_dog_inside,udf.udf_dog_vaccine,udf.udf_dog_disability,udf.udf_img,ud.dog_information')
            ->order('ud.dog_release_time desc')
            ->limit(($page-1)*10,10)
            ->select();
        return $data;
    }


    /**
     *getAdoptDetail  单条领养狗记录详情
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param  $dog_id 领养狗的id
     */
    public function getAdoptDetail(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_dog_family udf','ud.udf_id=udf.udf_id','left')
            ->field('ud.dog_id,uuu.user_id,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,ud.dog_release_time,ud.dog_apply_num,ud.dog_attention_num,ud.dog_adopt_status,ud.dog_adopt_experience,ud.dog_adopt_environment,ud.dog_adopt_sterilization,ud.dog_adpot_workers,ud.dog_adpot_protocol,ud.dog_adpot_certificate,ud.dog_adpot_information,ud.dog_information,udf.udf_name,ud.dog_img,udf.udf_id')
            ->where(['ud.dog_id'=>$dog_id])
            ->find();
        if($data['dog_img']){
            $img = trim($data['dog_img'],',');
            $data['dog_img'] = explode(',',$img);
        }
        //===========重新领养的历史记录==============
//        if($data['dog_adopt_status'] == 4){
            $repeat = Db::table('up_dog')
                ->alias('ud')
                ->join('up_u_user u1','ud.user_id=u1.user_id','left')
                ->join('up_u_user u2','ud.dog_adopt_new_userid=u2.user_id','left')
                ->field('u1.user_username fabuname,u2.user_username lyname,dog_adopt_number number,dog_update_time time')
                ->where(['ud.udf_id'=>$data['udf_id'],'dog_adopt_status'=>4])
                ->select();
//        }
        //=============end============
        //=======有多少人申请领养这个狗====
        $lynum = Db::table('up_dog_proposer')->where('dog_id',$dog_id)->count();
        //=========end===============
        switch($data['dog_adopt_status'])
        {
            case 1:
                $data['dog_adopt_status'] = "取消领养";
                break;
            case 2:
                $data['dog_adopt_status'] = "待领养";
                break;
            case 3:
                $data['dog_adopt_status'] = "完成领养";
                break;
            default:
                $data['dog_adopt_status'] = "重新领养";
        }
        $res = Db::table('up_u_user_interest_dynamic')
            ->where(['user_id'=>$user_id,'dog_id'=>$dog_id])
            ->find();
        if($res){
            $data['attention'] = 1;
        }else{
            $data['attention'] = 2;
        }
        Db::table("up_dog")->where(['dog_id'=>$dog_id])->setInc ("dog_click_num");
        return json_encode(['status'=>200,'msg'=>$data,'repeat'=>$repeat,'lynum'=>$lynum]);
    }


    /**
     *getProposerDeploy  领养信息申请页面配置信息获取
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 数据
     */
    public function getProposerDeploy(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $user = Db::table('up_u_user')
            ->where('user_id',$user_id)
            ->field('user_username,user_rel_name,user_gender,user_card')
            ->find();
        $year = substr($user['user_card'],6,4);
        $user['user_card'] = date('Y-m-d')-$year;
        if($user['user_gender'] == 1){
            $user['user_gender'] = "男";
        }else{
            $user['user_gender'] = "女";
        }
        //$user['user_username'] = $user['user_rel_name'];
        $cohabit = Db::table('up_proposer_cohabit')->select();
        $environment = Db::table('up_proposer_environment')->select();
        $house = Db::table('up_proposer_house')->select();
//        $petyet = Db::table('up_proposer_petyet')->select();
        $reason = Db::table('up_proposer_reason')->select();
        $care = Db::table('up_proposer_care')->select();
        $deploy = [
            'cohabit'=>$cohabit,
            'environment'=>$environment,
            'house'=>$house,
//            'petyet'=>$petyet,
            'reason'=>$reason,
            'care'=>$care,
            'user'=>$user
        ];

        return json_encode(['status'=>200,'msg'=>$deploy]);
    }

    /**
     *getProposerDataInsert  领养信息申请页面数据插入数据库
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 修改数据库状态
     */
    public function getProposerDataInsert(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $cohabit = ','.$cohabit.',';
        $reason = ','.$reason.',';
        $care = ','.$care.',';
        /*前台POST接收表单数据*/
        $data= [
            'proposer_user_id'=>$user_id,
            'proposer_abode_province'=>$abode_province,
            'proposer_abode_city'=>$abode_city,
            'proposer_abode_district' => $abode_district,
            'proposer_long_time' => $long_time,
            'proposer_experience' =>$experience,
            'proposer_cohabit' => $cohabit,
            'proposer_cohabit_other' => $cohabit_other,
            'proposer_environment' => $environment,
            'proposer_environment_other' => $environment_other,
            'proposer_house' => $house,
            'proposer_house_other' => $house_other,
            'proposer_petyet' => $petyet,
            'proposer_petyet_other' => $petyet_other,
            'proposer_reason' => $reason,
            'proposer_reason_other' => $reason_other,
            'proposer_care' => $care,
            'proposer_care_other' => $care_other,
            'proposer_other' => $proposer_other,
            'dog_id'=>$dog_id,
            'proposer_phone'=>$proposer_phone,
            'proposer_datetime'=>date('Y-m-d H:i:s'),
            'proposer_date'=>date('Y-m-d'),
            'proposer_year'=>date('Y'),
            'proposer_month'=>date('Y-m')
        ];
        $res = Db::table('up_dog_proposer')
            ->where(['dog_id'=>$dog_id,'proposer_user_id'=>$user_id,'proposer_status'=>1])
            ->find();
        if($res){
            return json_encode(['status'=>0,'msg'=>'不能重复申请！']);
        }
        /*用户id*/
        $att = Db::table('up_u_user')
            ->where(['user_id'=>$user_id])
            ->value('user_attestation');
        $att = 2;
        if($att == 2){
            $id = Db::table('up_dog')->where('dog_id',$dog_id)->value('user_id');
            if($id == $user_id){
                $returnData['status'] = 100;
                $returnData['msg'] = "您不能领养自己的狗！";
                return json_encode($returnData);
            }else{
                $res = Db::table('up_dog_proposer')->insert($data);
                if($res){
                    Db::table('up_dog')->where('dog_id',$dog_id)->setInc('dog_apply_num');
                    //=========================推送==========================
                    $who = Db::table('up_dog')->where('dog_id',$dog_id)->value('user_id');
                    $this->Jpush("{$who}",'有人申请领养',array('hello'=>'ioioiioi'));
                    //=========================end==========================
                    $returnData['status'] = 200;
                    $returnData['msg'] = "登记成功";
                    return json_encode($returnData);
                }else{
                    $returnData['status'] = 0;
                    $returnData['msg'] = "登记失败";
                    return json_encode($returnData);
                }
            }
        }else{
            $returnData['status'] = 100;
            $returnData['msg'] = "您还尚未认证！";
            return json_encode($returnData);
        }

    }

    /*=====领养End=====*/

    /*=====寻狗Start=====*/
    /**
     * getfindDogPlace 获取寻狗区域
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param $shi 市
     * @return 返回数组
     */
    public function getfindDogPlace(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $place = Db::table('up_area')
            ->where(['pid'=>$shi])
            ->select();
        return json_encode(['status'=>200,'msg'=>$place]);
    }
    /**
     * findDog 获取寻狗展示数据
     * @param $shi 市
     * @param $type 1代表“距离最近” 2代表“最新发布”
     * @param $qu 区
     * @param string $habitus 体型
     * @param string $hair 长毛、短毛
     * @param string $sex 公、母
     * @param string $tail 尾巴
     * @param string $lon 经度
     * @param string $lat 纬度
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param $page 页码
     * @return 返回数组
     */
    public function findDog()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        /*区*/
        if($qu !== ""){
            $where['ud.area_qu'] = $qu;
        }
        /*寻狗*/
        $where['ud.dog_type'] = 4;
        /*用户指定的市*/
        $where['ud.area_shi'] = $shi;
        /*已发布的*/
        $where['ud.dog_preserve'] = 2;
        $where['ud.dog_seek_status'] = 1;
        /*前台获取的特征数据*/
        //体型
        if($habitus !== ""){
            $where['uds.udskc_habitus'] = $habitus;
        }
        //长毛、短毛
        if($hair !== ""){
            $where['uds.udskc_hair'] = $hair;
        }
        //性别
        if($sex !== ""){
            $where['uds.udskc_sex'] = $sex;
        }
        //尾巴
        if($tail !== ""){
            $where['uds.udskc_tail'] = $tail;
        }
        /*默认最近发布的*/
        $sort = "ud.dog_release_time desc";
        $field = 'ud.dog_id,ud.dog_img,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,ua.district shi,uaa.district qu,ud.dog_release_time,udf.udf_name,udf.udf_img,ud.dog_seek_time,ud.dog_add,ud.dog_seek_status,uds.udskc_habitus,uds.udskc_hair,uds.udskc_sex,uds.udskc_tail,uds.udskca_id,uds.udskca_id_other,uds.udskci_id,uds.udskci_id_other';
        if($type == 1 && $lon!=="" && $lat!==""){
            $field = 'ud.dog_id,ud.dog_img,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,ua.district shi,uaa.district qu,ud.dog_release_time,udf.udf_name,udf.udf_img,ud.dog_seek_time,ud.dog_add,ud.dog_seek_status,uds.udskc_habitus,uds.udskc_hair,uds.udskc_sex,uds.udskc_tail,uds.udskca_id,uds.udskca_id_other,uds.udskci_id,uds.udskci_id_other,ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(('.$lat.'* PI() / 180 - ud.dog_lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(ud.dog_lat * PI() / 180) * POW(SIN(('.$lon.' * PI() / 180 - ud.dog_lon * PI() / 180) / 2),2))) * 1000) AS distance';
            $sort = "distance asc";
        }
        $arr = $this->getSeekDogData($where,$sort,$field,$page);
        for($i=0;$i<count($arr);$i++){
            switch($arr[$i]['dog_seek_status'])
            {
                case 1:
                    $arr[$i]['dog_seek_status'] = "寻找中";
                    break;
                case 2:
                    $arr[$i]['dog_seek_status'] = "已找回";
                    break;
                default:
                    $arr[$i]['dog_seek_status'] = "暂停寻找";
            }
            switch($arr[$i]['udskc_habitus'])
            {
                case 1:
                    $arr[$i]['udskc_habitus'] = "小型狗";
                    break;
                case 2:
                    $arr[$i]['udskc_habitus'] = "中型狗";
                    break;
                default:
                    $arr[$i]['udskc_habitus'] = "大型狗";
            }
            switch($arr[$i]['udskc_hair'])
            {
                case 1:
                    $arr[$i]['udskc_hair'] = "长毛";
                    break;
                default:
                    $arr[$i]['udskc_hair'] = "短毛";
            }
            switch($arr[$i]['udskc_tail'])
            {
                case 1:
                    $arr[$i]['udskc_tail'] = "长尾";
                    break;
                case 2:
                    $arr[$i]['udskc_tail'] = "短尾";
                    break;
                default:
                    $arr[$i]['udskc_tail'] = "无尾巴";
            }
            switch($arr[$i]['udskc_sex'])
            {
                case 1:
                    $arr[$i]['udskc_sex'] = "公";
                    break;
                default:
                    $arr[$i]['udskc_sex'] = "母";
            }
        }
        return json_encode(['status'=>200,'msg'=>$arr]);
    }

    /**
     *getSeekDogData 获取寻狗首页数据
     * @return 返回数据
     */
    public function getSeekDogData($where,$sort,$field,$page){
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_area ua','ud.area_shi=ua.district_id','left')
            ->join('up_area uaa','ud.area_qu=uaa.district_id','left')
            ->join('up_dog_family udf','ud.udf_id=udf.udf_id','left')
            ->join('up_dog_seek_characteristic uds','ud.dog_seek_characteristic=uds.udskc_id','left')
            ->where($where)
            ->field($field)
            ->order($sort)
            ->limit(($page-1)*10,10)
            ->select();
        for($i = 0;$i<count($data);$i++){
            $b = Db::table('up_dog_seek_characteristic_apparel')
                ->where('udskca_id','IN',$data[$i]['udskca_id'])
                ->column('udskca_name');
            $a = Db::table('up_dog_seek_characteristic_other')
                ->where('udsci_id','IN',$data[$i]['udskci_id'])
                ->column('udsci_name');
            $b_num = explode(',',$data[$i]['udskca_id']);
            $a_num = explode(',',$data[$i]['udskci_id']);
            if(in_array(0,$b_num)){
                $b['other'] = $data[$i]['udskca_id_other'];
            }
            if(in_array(0,$a_num)){
                $a['other'] = $data[$i]['udskci_id_other'];
            }

            $data[$i]['apparel'] = $b;
            $data[$i]['others'] = $a;
        }
        return $data;
    }


    /**
     *getSeekDogDataDetail 获取单条寻狗详情
     * @param $dog_id狗id
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数据
     */
    public function getSeekDogDataDetail(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_area ua','ud.area_shi=ua.district_id','left')
            ->join('up_area uaa','ud.area_qu=uaa.district_id','left')
            ->join('up_dog_family udf','ud.udf_id=udf.udf_id','left')
            ->join('up_dog_seek_characteristic uds','ud.dog_seek_characteristic=uds.udskc_id')
//            ->join('up_dog_miss_reason umr','ud.dog_seek_so=umr.reason_id')
            ->where(['dog_id'=>$dog_id])
            ->field('uuu.user_id,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,ua.district shi,uaa.district qu,ud.dog_release_time,udf.udf_name,udf.udf_img,ud.dog_seek_time,ud.dog_add,ud.dog_seek_status,ud.dog_information,ud.dog_img,ud.dog_seek_people,ud.dog_seek_phone,ud.dog_seek_money_xx,ud.dog_seek_money_ok,ud.dog_seek_so,ud.dog_seek_so_qita,ud.dog_attention_num,uds.udskc_habitus,uds.udskc_hair,uds.udskc_sex,uds.udskc_tail,uds.udskca_id,uds.udskca_id_other,uds.udskci_id,uds.udskci_id_other,udf.udf_id')
            ->find();
        if($data['dog_img']){
            $img = trim($data['dog_img'],',');
            $data['dog_img'] = explode(',',$img);
        }
		//else{
        //    if($data['udf_img']){
        //        $img = trim($data['udf_img'],',');
        //        $data['dog_img'] = explode(',',$img);
        //    }
        //}
        if($data['udskca_id'] !=="" && $data['udskci_id'] !==""){
            $b = Db::table('up_dog_seek_characteristic_apparel')
                ->where('udskca_id','IN',$data['udskca_id'])
                ->column('udskca_name');
            $a = Db::table('up_dog_seek_characteristic_other')
                ->where('udsci_id','IN',$data['udskci_id'])
                ->column('udsci_name');
            $b_num = explode(',',$data['udskca_id']);
            $a_num = explode(',',$data['udskci_id']);
            if(in_array(0,$b_num)){
                $b['other'] = $data['udskca_id_other'];
            }
            if(in_array(0,$a_num)){
                $a['other'] = $data['udskci_id_other'];
            }

            $data['apparel'] = $b;
            $data['others'] = $a;
        }

        if($data['dog_seek_so'] == 0){
            $data['reason_name'] = $data['dog_seek_so_qita'];
        }else{
            $data['reason_name'] = Db::table('up_dog_miss_reason')->where('reason_id',$data['dog_seek_so'])->value('reason_name');
        }
            switch($data['dog_seek_status'])
            {
                case 1:
                    $data['dog_seek_status'] = "寻找中";
                    break;
                case 2:
                    $data['dog_seek_status'] = "已找回";
                    break;
                case 3:
                    $data['dog_seek_status'] = "暂停寻找";
                    break;
            }
            switch($data['udskc_habitus'])
            {
                case 1:
                    $data['udskc_habitus'] = "小型狗";
                    break;
                case 2:
                    $data['udskc_habitus'] = "中型狗";
                    break;
                case 3:
                    $data['udskc_habitus'] = "大型狗";
                    break;
                default:
                    $data['udskc_habitus'] = "";
            }
            switch($data['udskc_hair'])
            {
                case 1:
                    $data['udskc_hair'] = "长毛";
                    break;
                case 2:
                    $data['udskc_hair'] = "短毛";
                    break;
                default:
                    $data['udskc_hair'] = "";
            }
            switch($data['udskc_tail'])
            {
                case 1:
                    $data['udskc_tail'] = "长尾";
                    break;
                case 2:
                    $data['udskc_tail'] = "短尾";
                    break;
                case 3:
                    $data['udskc_tail'] = "无尾巴";
                    break;
                default:
                    $data['udskc_tail'] = "";
            }
            switch($data['udskc_sex'])
            {
                case 1:
                    $data['udskc_sex'] = "公";
                    break;
                case 2:
                    $data['udskc_sex'] = "母";
                    break;
                default:
                    $data['udskc_sex'] = "";
            }
        $res = Db::table('up_u_user_interest_dynamic')
            ->where(['user_id'=>$user_id,'dog_id'=>$dog_id])
            ->find();
        if($res){
            $data['attention'] = 1;
        }else{
            $data['attention'] = 2;
        }
        Db::table("up_dog")->where(['dog_id'=>$dog_id])->setInc ("dog_click_num");
        return json_encode(['status'=>200,'msg'=>$data]);

    }

    /**
     * getDogInformation 获取狗的主页信息
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param $udf_id 狗对应id
     */
    public function getDogInformation(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_dog_family')
            ->alias('udf')
            ->join('up_area u','udf.udf_province=u.district_id','left')
            ->join('up_area ua','udf.udf_city=ua.district_id','left')
            ->join('up_area uaa','udf.udf_district=uaa.district_id','left')
            ->where('udf_id',$udf_id)
            ->field('udf.udf_img,udf_id,udf.udf_name,udf.udf_sex,udf.udf_age,udf.udf_size,u.district shen,ua.district shi,uaa.district qu,udf.udf_condition,udf.udf_character,udf.udf_character_other,udf.udf_dog_vaccine,udf.udf_dog_outside,udf.udf_dog_inside,udf.udf_dog_sterilization,udf.udf_dog_disability,udf.udf_story')
            ->find();
        $b = Db::table('up_dog_character')
            ->where('uco_id','IN',$data['udf_character'])
            ->column('uco_character');
        $num = explode(',',$data['udf_character']);
        if(in_array(0,$num)){
            $b['other'] = $data['udf_character_other'];
        }
        $data['character'] = $b;
        $num2 = Db::table('up_u_user_interest_dog')
            ->where(['udf_id'=>$udf_id])
            ->count();
        $data['attention_num'] = $num2;
        $atr = Db::table('up_dog_family')
            ->where(['udf_id'=>$udf_id,'user_id'=>$user_id])
            ->find();
        if($atr){
            /*属于我的*/
            $data['belong'] = 1;
        }else{
            /*属于别人的*/
            $data['belong'] = 2;
        }
        $res = Db::table('up_u_user_interest_dog')
            ->where(['user_id'=>$user_id,'udf_id'=>$udf_id])
            ->find();
        if($res){
            /*已关注*/
            $data['attention'] = 1;
        }else{
            /*未关注*/
            $data['attention'] = 2;
        }
        $img = trim($data['udf_img'],',');
        if($img){
            $data['udf_img'] = explode(',',$img);
        }
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /*=====寻狗End=====*/

    /*=====狗窝Start=====*/
    /**
     * getMyDogData 属于用户的所有狗
     * @param $time 时间
     * @param $value 加密值
     * @param $user_id 用户id
     */
    public function getMyDogData(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $where = array();
        $where['user_id'] = $user_id;
        if(isset($keyword) && $keyword){
            $where['udf_name'] = ['like',"%$keyword%"];
        }
        $data = Db::table('up_dog_family')
            ->where($where)
            ->field('udf_id,udf_name,udf_img,udf_sex,udf_age')
            ->order("udf_datetime desc")
            ->page($page,10)
            ->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * getCharacter 获取狗性格
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 数据
     */
    public function getDogCharacter(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_dog_character')
            ->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * NewlyIncreasedDog 新增狗窝的狗
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 状态
     */
    public function NewlyIncreasedDog(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $udf_name = trim($udf_name);
        if($udf_name == ""){
            $returnData['status'] = 0;
            $returnData['msg'] = "狗名字不能为空！";
            return json_encode($returnData);
        }else{
            /*前台收集到的表单数据*/
            $data = [
                'udf_name'=>$udf_name,
                'udf_province'=>$province,
                'udf_city'=>$city,
                'udf_district'=>$district,
                'udf_sex'=>$sex,
                'udf_size'=>$size,
                'udf_age'=>$age,
                'udf_character'=>$character,
                'udf_dog_vaccine'=>$vaccine,
                'udf_dog_outside'=>$outside,
                'udf_dog_inside'=>$inside,
                'udf_dog_sterilization'=>$sterilization,
                'udf_dog_disability'=>$disability,
                'udf_story'=>$story,
                'udf_img'=>$udf_img,
                'user_id'=>$user_id,
                'udf_character_other'=>$character_other,
                'udf_time'=>date('Y-m-d'),
                'udf_year'=>date("Y"),
                'udf_month'=>date("Y-m"),
                'udf_datetime'=>date('Y-m-d H:i:s')
            ];
            if($condition !=""){
                $data['udf_condition'] = $condition;
            }
            $res = Db::table('up_dog_family')
                ->insert($data);
            if($res){
                $returnData['status'] = 200;
                $returnData['msg'] = "新增成功！";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "新增失败！";
                return json_encode($returnData);
            }
        }
    }
    /**
     * deleteMyDog 删除狗窝的狗
     * @param $udf_id 狗的id
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数据
     */
    public function deleteMyDog(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $sta = Db::table('up_dog')
            ->where('udf_id',$udf_id)
            ->select();
        if($sta){
            $returnData['status'] = 100;
            $returnData['msg'] = "不允许删除！";
            return json_encode($returnData);
        }
        $res = Db::table('up_dog_family')
            ->delete($udf_id);
        if($res){
            $returnData['status'] = 200;
            $returnData['msg'] = "删除成功！";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "删除失败！";
            return json_encode($returnData);
        }
    }
    /**
     * updateDogInformation 要修改狗的详细信息
     * @param $udf_id 狗的id
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数据
     */
    public function updateDogInformation(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_dog_family')
            ->alias ('udf')
            ->join('up_area u','udf.udf_province=u.district_id','left')
            ->join('up_area ua','udf.udf_city=ua.district_id','left')
            ->join('up_area uaa','udf.udf_district=uaa.district_id','left')
            ->where('udf_id',$udf_id)
            ->field('u.district shen,ua.district shi,uaa.district qu,udf.*')
            ->find();
        if($data['udf_img']){
            $udf_img = trim($data['udf_img'],',');
            $img = explode (',',$udf_img);
            $data['udf_img'] = $img;
        }
        $b = Db::table('up_dog_character')
            ->where('uco_id','IN',$data['udf_character'])
            ->column('uco_character');
        $b_num = explode(',',$data['udf_character']);
        if(in_array(0,$b_num)){
            $data['other'] = $data['udf_character_other'];
        }
        $data['character'] = $b;
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * updateDog 修改狗窝狗的信息
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 状态
     */
    public function updateDog(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        /*前台收集到的表单数据*/
        $data = [
            'udf_name'=>$udf_name,
            'udf_province'=>$province,
            'udf_city'=>$city,
            'udf_district'=>$district,
            'udf_sex'=>$sex,
            'udf_size'=>$size,
            'udf_age'=>$age,
            'udf_character'=>$character,
            'udf_dog_vaccine'=>$vaccine,
            'udf_dog_outside'=>$outside,
            'udf_dog_inside'=>$inside,
            'udf_dog_sterilization'=>$sterilization,
            'udf_dog_disability'=>$disability,
            'udf_story'=>$story,
            'udf_img'=>$udf_img,
            'user_id'=>$user_id,
            'udf_character_other'=>$character_other,
            'udf_updatetime'=>date("Y-m-d H:i:s")
        ];
        if($condition != ""){
            $data['udf_condition'] = $condition;
        }
        // 启动事务
        Db::startTrans();
        try{
            Db::table('up_dog_family')->where(['udf_id'=>$udf_id])->update($data);
            // 提交事务
            Db::commit();
            $returnData['status'] = 200;
            $returnData['msg'] = "修改成功！";
            return json_encode($returnData);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $returnData['status'] = 200;
            $returnData['msg'] = "修改失败！";
            return json_encode($returnData);
        }

    }
    /*=====狗窝End=====*/

    /*=====发布Start=====*/
    /**
     * getCharacteristicData 获取流浪狗特征
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数据
     */
    public function getCharacteristicData(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_dog_strat_characteristic')
            ->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * NewlyVagrancyDog 新增流浪狗
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数据
     */
    public function NewlyVagrancyDog(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $udsc_id = ','.$udsc_id.',';
        /*前台收集到的表单数据*/
        $data = [
            'dog_type'=>1,
            'user_id'=>$user_id,
            'area_sheng'=>$sheng,
            'area_shi'=>$shi,
            'area_qu'=>$qu,
            'dog_add'=>$add,
            'dog_num'=>$dog_num,
            'udsc_id'=>$udsc_id,
            'dog_information'=>$dog_information,
            'dog_img'=>$dog_img,
            'dog_status'=>1,
            'dog_preserve'=>2,
            'dog_lon'=>$lon,
            'dog_lat'=>$lat,
            'udsc_id_information'=>$udsc_id_information,
            'dog_time'=>date('Y-m-d'),
            'dog_year'=>date('Y'),
            'dog_month'=>date('Y-m')
        ];
        $time = date('Y-m-d H:i:s');
        $data['dog_release_time'] = $time;
        $data['dog_update_time'] = $time;
        // 启动事务
        Db::startTrans();
        try{
            Db::table('up_dog')
                ->insert($data);
            $res = Db::table('up_integral')
                ->where(['integral_status'=>3,'integral_user_id'=>$user_id])
                ->whereTime('integral_time', 'today')
                ->count();
            if($res<2){
                Db::table('up_integral')
                    ->insert(['integral_status'=>3,'integral_user_id'=>$user_id,'integral_time'=>date('Y-m-d H:i:s'),'integral_num'=>5]);
                Db::table('up_u_user')
                    ->where('user_id',$user_id)
                    ->setInc('user_integral',5);
            }
            // 提交事务
            Db::commit();
            $returnData['status'] = 200;
            $returnData['msg'] = "新增成功！";
            return json_encode($returnData);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $returnData['status'] = 0;
            $returnData['msg'] = "新增失败！";
            return json_encode($returnData);
        }
    }

    /**
     * getMutualHelp 获取互助的类别
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数据
     */
    public function getMutualHelp(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_dog_heo')
            ->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * NewlyHelpDog 新增互助
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param $type 1:发布  2:暂存
     * @return 返回数据
     */
    public function NewlyHelpDog(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
//        $sta = Db::table('up_u_user')
//            ->where('user_id',$user_id)
//            ->value('user_attestation');
//        if($sta !== 2){
//            $returnData['status'] = 100;
//            $returnData['msg'] = "此服务需实名认证通过后才可使用！";
//            return json_encode($returnData);
//        }
        $help_style = ','.$help_style.',';
        if($dog_img == ""){
            if($type == 1){
                $img = Db::table('up_dog_family')
                    ->where('udf_id',$udf_id)
                    ->value('udf_img');
                if($img){
                    $dog_img = trim($img,',');
                }
            }
        }
        /*前台收集到的表单数据*/
        $data = [
            'dog_type'=>2,
            'user_id'=>$user_id,
            'udf_id'=>$udf_id,
            'area_sheng'=>$sheng,
            'area_shi'=>$shi,
            'area_qu'=>$qu,
            'dog_help_style'=>$help_style,
            'dog_end_time'=>$end_time,
            'dog_img'=>$dog_img,
            'dog_lon'=>$lon,
            'dog_lat'=>$lat,
            'dog_help_information' => $help_information,
            'dog_help_status'=>1,
            'dog_information'=>$information,
            'dog_year'=>date('Y'),
            'dog_month'=>date('Y-m'),
            'dog_time' => date('Y-m-d')
        ];
        $time = date('Y-m-d H:i:s');
        if($type == 1){
            $data['dog_preserve'] = 2;
            $data['dog_release_time'] = $time;
            $data['dog_update_time'] = $time;
            // 启动事务
            Db::startTrans();
            try{
                $dog_id = Db::table('up_dog')
                    ->insertGetId($data);
                $res = Db::table('up_integral')
                    ->where(['integral_status'=>3,'integral_user_id'=>$user_id])
                    ->whereTime('integral_time', 'today')
                    ->count();
                if($res<2){
                    Db::table('up_integral')
                        ->insert(['integral_status'=>3,'integral_user_id'=>$user_id,'integral_time'=>date('Y-m-d H:i:s'),'integral_num'=>5,'integral_dog_id'=>$dog_id]);
                    Db::table('up_u_user')
                        ->where('user_id',$user_id)
                        ->setInc('user_integral',5);
                }
                // 提交事务
                Db::commit();
                $returnData['status'] = 200;
                $returnData['msg'] = "新增成功！";
                return json_encode($returnData);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $returnData['status'] = 0;
                $returnData['msg'] = "新增失败！";
                return json_encode($returnData);
            }
        }else if($type == 2){
            $data['dog_preserve'] = 1;
            $data['dog_update_time'] = $time;
            $data['dog_release_time'] = $time;
            $res = Db::table('up_dog')
                ->insert($data);
            if($res){
                $returnData['status'] = 200;
                $returnData['msg'] = "新增成功！";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "新增失败！";
                return json_encode($returnData);
            }
        }

    }

    /**
     * NewlyAdopt 新增领养
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回状态
     */
    public function NewlyAdopt(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
//        $sta = Db::table('up_u_user')
//            ->where('user_id',$user_id)
//            ->value('user_attestation');
//        if($sta !== 2){
//            $returnData['status'] = 100;
//            $returnData['msg'] = "此服务需实名认证通过后才可使用！";
//            return json_encode($returnData);
//        }
        /*前台收集到的表单数据*/
        $data = [
            'dog_type'=>3,
            'user_id'=>$user_id,
            'udf_id'=>$udf_id,
            'area_sheng'=>$sheng,
            'area_shi'=>$shi,
            'area_qu'=>$qu,
            'dog_adopt_experience'=>$experience,
            'dog_adopt_environment'=>$environment,
            'dog_adopt_sterilization'=>$sterilization,
            'dog_adpot_workers'=>$workers,
            'dog_adpot_protocol'=>$protocol,
            'dog_adpot_certificate'=>$certificate,
            'dog_adpot_information'=>$adpot_information,
            'dog_information'=>$information,
            'dog_adopt_status'=>2,
            'dog_year'=>date('Y'),
            'dog_month'=>date('Y-m'),
            'dog_time' => date('Y-m-d')
        ];
        $data['dog_img'] = '';
        if($udf_id){
            $img = Db::table('up_dog_family')
                ->where('udf_id',$udf_id)
                ->value('udf_img');
            if($img){
                $data['dog_img'] = trim($img,',');
            }
        }
        if($type == 1){
            $where = [
                'udf_id'=>$udf_id,
                'dog_type'=>3
            ];
            $str = Db::table('up_dog')
                ->where($where)
                ->where('dog_adopt_status','IN',[2,3])
                ->find();
            if($str){
                $returnData['status'] = 100;
                $returnData['msg'] = "不能重复发布领养！";
                return json_encode($returnData);
            }
        }

        $time = date('Y-m-d H:i:s');
        if($type == 1){
            $data['dog_preserve'] = 2;
            $data['dog_release_time'] = $time;
            $data['dog_update_time'] = $time;
            // 启动事务
            Db::startTrans();
            try{
                $dog_id = Db::table('up_dog')
                    ->insertGetId($data);
                $res = Db::table('up_integral')
                    ->where(['integral_status'=>3,'integral_user_id'=>$user_id])
                    ->whereTime('integral_time', 'today')
                    ->count();
                if($res<2){
                    Db::table('up_integral')
                        ->insert(['integral_status'=>3,'integral_user_id'=>$user_id,'integral_time'=>date('Y-m-d H:i:s'),'integral_num'=>5,'integral_dog_id'=>$dog_id]);
                    Db::table('up_u_user')
                        ->where('user_id',$user_id)
                        ->setInc('user_integral',5);
                }
                //=============自动插入两条默认领养人=============
                $pro = array();
                $pro['proposer_user_id'] = -1;
                $pro['dog_id'] = $dog_id;
                $pro['proposer_status'] = 1;
                Db::table('up_dog_proposer')->insert($pro);
                $pro2 = array();
                $pro2['proposer_user_id'] = -2;
                $pro2['dog_id'] = $dog_id;
                $pro2['proposer_status'] = 1;
                Db::table('up_dog_proposer')->insert($pro2);
                //===================end======================
                // 提交事务
                Db::commit();
                $returnData['status'] = 200;
                $returnData['msg'] = "发布成功！";
                return json_encode($returnData);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $returnData['status'] = 0;
                $returnData['msg'] = "发布失败！";
                return json_encode($returnData);
            }
        }else if($type == 2){
            $data['dog_preserve'] = 1;
            $data['dog_release_time'] = $time;
            $data['dog_update_time'] = $time;
            $res = Db::table('up_dog')
                ->insertGetId($data);
            //=============自动插入两条默认领养人=============
            $pro = array();
            $pro['proposer_user_id'] = -1;
            $pro['dog_id'] = $res;
            $pro['proposer_status'] = 1;
            Db::table('up_dog_proposer')->insert($pro);
            $pro2 = array();
            $pro2['proposer_user_id'] = -2;
            $pro2['dog_id'] = $res;
            $pro2['proposer_status'] = 1;
            Db::table('up_dog_proposer')->insert($pro2);
            //===================end======================
            if($res){
                $returnData['status'] = 200;
                $returnData['msg'] = "发布成功！";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "发布失败！";
                return json_encode($returnData);
            }
        }
    }

    /**
     * getSearchCharacteristic 获取寻狗特征
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数据
     */
    public function getSearchCharacteristic(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        //获取失踪原因
        $Reason = Db::table('up_dog_miss_reason')
            ->select();
        //获取穿戴特征
        $Apparel = Db::table('up_dog_seek_characteristic_apparel')
            ->select();
        //获取其他特征
        $Other = Db::table('up_dog_seek_characteristic_other')
            ->select();
        $data = [
            'reason'=>$Reason,
            'apparel'=>$Apparel,
            'other'=>$Other
        ];
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * NewlySearch 新增寻狗
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回状态
     */
    public function NewlySearch(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }

//        $sta = Db::table('up_u_user')
//            ->where('user_id',$user_id)
//            ->value('user_attestation');
//        if($sta !== 2){
//            $returnData['status'] = 100;
//            $returnData['msg'] = "此服务需实名认证通过后才可使用！";
//            return json_encode($returnData);
//        }
        // 启动事务
        Db::startTrans();
        try{
            /*前台收集到的表单数据 寻狗特征*/
            $arr = [
                'udskc_sex'=>$udskc_sex,
                'udskc_habitus'=>$udskc_habitus,
                'udskc_hair'=>$udskc_hair,
                'udskc_tail'=>$udskc_tail,
                'udskca_id'=>$udskca_id,
                'udskci_id'=>$udskci_id,
                'udskca_id_other'=>$udskca_id_other,
                'udskci_id_other'=>$udskci_id_other
            ];
            $udskc_id = Db::table('up_dog_seek_characteristic')->insertGetId($arr);
            /*前台收集到的表单数据 发布寻狗*/
            if($dog_img == ""){
                if($type == 1){
                    $img = Db::table('up_dog_family')
                        ->where('udf_id',$udf_id)
                        ->value('udf_img');
                    if($img){
                        $dog_img = trim($img,',');
                    }
                }
            }
            $data = [
                'dog_type'=>4,
                'user_id'=>$user_id,
                'udf_id'=>$udf_id,
                'area_sheng'=>$sheng,
                'area_shi'=>$shi,
                'area_qu'=>$qu,
                'dog_add'=>$add,
                'dog_information'=>$dog_information,
                'dog_seek_time'=>$seek_time,
                'dog_seek_so'=>$seek_so,
                'dog_seek_people'=>$seek_people,
                'dog_seek_phone'=>$seek_phone,
                'dog_seek_money_xx'=>$seek_money_xx,
                'dog_seek_money_ok'=>$seek_money_ok,
                'dog_seek_status'=>1,
                'dog_lon'=>$lon,
                'dog_lat'=>$lat,
                'dog_seek_so_qita'=>$seek_so_qita,
                'dog_seek_date'=>substr($seek_time,0,10),
                'dog_img'=>$dog_img,
                'dog_year'=>date('Y'),
                'dog_month'=>date('Y-m'),
                'dog_time' => date('Y-m-d')
            ];
            $data['dog_seek_characteristic'] = $udskc_id;
            $time = date('Y-m-d H:i:s');
            if($type == 1){
                $data['dog_preserve'] = 2;
                $data['dog_release_time'] = $time;
                $data['dog_update_time'] = $time;

                $dog_id = Db::table('up_dog')
                    ->insertGetId($data);
                $res = Db::table('up_integral')
                    ->where(['integral_status'=>3,'integral_user_id'=>$user_id])
                    ->whereTime('integral_time', 'today')
                    ->count();
                if($res<2){
                    Db::table('up_integral')
                        ->insert(['integral_status'=>3,'integral_user_id'=>$user_id,'integral_time'=>date('Y-m-d H:i:s'),'integral_num'=>5,'integral_dog_id'=>$dog_id]);
                    Db::table('up_u_user')
                        ->where('user_id',$user_id)
                        ->setInc('user_integral',5);
                }
            }else if($type == 2){
                $data['dog_preserve'] = 1;
                $data['dog_release_time'] = $time;
                $data['dog_update_time'] = $time;
                Db::table('up_dog')
                    ->insert($data);
            }
            // 提交事务
            Db::commit();
            $returnData['status'] = 200;
            $returnData['msg'] = "发布成功！";
            return json_encode($returnData);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $returnData['status'] = 0;
            $returnData['msg'] = "发布失败！";
            return json_encode($returnData);
        }
    }
    /*====发布End=====*/

    /*====关注Start=====*/
    /**
     * attentionDog 关注狗OR取消关注狗
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回状态
     */
    public function attentionDog(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        if($user_id == "" || $user_id < 1){
            return json_encode(['status'=>100,"msg"=>"请先登录！"]);
        }else{
            $data = [
                'user_id'=>$user_id,
                'udf_id'=>$udf_id
            ];
            $have = Db::table('up_u_user_interest_dog')->where($data)->find();
            if($have){
                $res = Db::table('up_u_user_interest_dog')
                    ->where($data)
                    ->delete();
                if($res){
                    $returnData['status'] = 200;
                    $returnData['msg'] = "取消关注成功！";
                    return json_encode($returnData);
                }else{
                    $returnData['status'] = 0;
                    $returnData['msg'] = "取消关注失败！";
                    return json_encode($returnData);
                }
            }else{
                $data['ud_datetime'] = date('Y-m-d H:i:s');
                $res = Db::table('up_u_user_interest_dog')
                    ->insert($data);
                if($res){
                    $returnData['status'] = 200;
                    $returnData['msg'] = "关注成功！";
                    return json_encode($returnData);
                }else{
                    $returnData['status'] = 0;
                    $returnData['msg'] = "关注失败！";
                    return json_encode($returnData);
                }
            }
        }
    }


    /**
     * attentionDynamic 关注狗动态OR取消关注狗动态
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param $type 1代表关注  2代表取消关注
     * @return 返回状态
     */
    public function attentionDynamic(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        if($user_id == "" || $user_id < 1){
            return json_encode(['status'=>100,"msg"=>"请先登录！"]);
        }else{
            $where = [
                'user_id'=>$user_id,
                'dog_id'=>$dog_id
            ];
            $have = Db::table('up_u_user_interest_dynamic')->where($where)->find();
            if($have){
                Db::startTrans();
                try{
                    Db::table('up_u_user_interest_dynamic')
                        ->where($where)
                        ->delete();
                    Db::table('up_dog')->where('dog_id', $dog_id)->setDec('dog_attention_num');
                    // 提交事务
                    Db::commit();
                    $returnData['status'] = 200;
                    $returnData['msg'] = "取消关注成功！";
                    return json_encode($returnData);
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    $returnData['status'] = 0;
                    $returnData['msg'] = "取消关注失败！";
                    return json_encode($returnData);
                }
            }else{
                Db::startTrans();
                try{
                    Db::table('up_u_user_interest_dynamic')
                        ->insert($where);
                    Db::table('up_dog')
                        ->where('dog_id', $dog_id)
                        ->setInc('dog_attention_num');
                    // 提交事务
                    Db::commit();
                    $returnData['status'] = 200;
                    $returnData['msg'] = "关注成功！";
                    return json_encode($returnData);
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    $returnData['status'] = 0;
                    $returnData['msg'] = "关注失败！";
                    return json_encode($returnData);
                }
            }
        }
    }
    /*====关注End=====*/

    /*====Banner图Start====*/

    /**
     * getBannerPicture 获取首页Banner图
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数组数据
     */
    public function getBannerPicture(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_banner')
            ->where('banner_status',1)
            ->field('banner_id,banner_img,banner_title,banner_time,banner_type,banner_href')
            ->limit(6)
            ->order('banner_sort')
            ->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * 获取banner详情
     * banner_id
     * @return string
     */
    public function getBannerInfo(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_banner')
            ->where('banner_id',$banner_id)
            ->find();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /*====Banner图End=====*/

    /*====留言&回复Start=====*/
    /**
     * leaveMessage 动态留言
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回状态
     */
    public function leaveMessage(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $time = date('Y-m-d H:i:s');
        $data = [
            'm_publish_id'=>$publish_id,
            'm_reply_id'=>$reply_id,
            'm_dog_type'=>$dog_type,
            'm_dog_id'=>$dog_id,
            'm_message'=>$message,
            'm_status'=>0,
            'm_datetime'=>$time
        ];
        // 启动事务
        Db::startTrans();
        try{
            Db::table('up_message')
                ->insert($data);
            $res = Db::table('up_integral')
                ->where(['integral_status'=>4,'integral_user_id'=>$user_id])
                ->whereTime('integral_time', 'today')
                ->count();
            if($res<10){
                Db::table('up_integral')
                    ->insert(['integral_status'=>4,'integral_user_id'=>$user_id,'integral_dog_id'=>$dog_id,'integral_time'=>date('Y-m-d H:i:s'),'integral_num'=>1]);
                Db::table('up_u_user')
                    ->where('user_id',$user_id)
                    ->setInc('user_integral',1);
            }
            // 提交事务
            Db::commit();
            //极光推送
            $this->Jpush("{$publish_id}",'有人给您留言了',array('hello'=>'ioioiioi'));
            //end
            $returnData['status'] = 200;
            $returnData['msg'] = "留言成功！";
            return json_encode($returnData);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $returnData['status'] = 0;
            $returnData['msg'] = "留言失败！";
            return json_encode($returnData);
        }
    }

    /**
     * replyMessage 留言回复
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回状态
     */
    public function replyMessage(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = [
            'm_id'=>$m_id,
            'm_message_reply'=>$reply,
            'm_status_reply'=>1,
            'm_reply_datetime'=>date('Y-m-d H:i:s')
        ];
        $res = Db::table('up_message')
            ->where('m_id',$m_id)
            ->update($data);
        if($res){
            //极光推送
            $juid = Db::table('up_message')
                ->where('m_id',$m_id)
                ->value('m_reply_id');
            $this->Jpush("{$juid}",'有人给您回复了',array('hello'=>'ioioiioi'));
            //end
            $returnData['status'] = 200;
            $returnData['msg'] = "回复成功！";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "回复失败！";
            return json_encode($returnData);
        }
    }
    /*====留言&回复End=====*/
    /*====举报Start=====*/
    /**
     * getReportName 举报配置文件
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数组
     */
    public function getReportName(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_report_name')
            ->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * tipOff 举报
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回状态
     */
    public function tipOff(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
//        $sta = Db::table('up_u_user')
//            ->where('user_id',$user_id)
//            ->value('user_attestation');
//        if($sta !== 2){
//            $returnData['status'] = 100;
//            $returnData['msg'] = "通过实名认证后才可进行举报！";
//            return json_encode($returnData);
//        }
        $data = [
            'report_time'=>date('Y-m-d H:i:s'),
            'report_business'=>$business,
            'report_user_id'=>$user_id,
            'report_dog_id'=>$dog_id,
            'report_cover_user_id'=>$cover_id,
            'report_type'=>$report_type,
            'report_reason'=>$report_reason
            ];
        $res = Db::table('up_report')
            ->insert($data);
        if($res){
            $returnData['status'] = 200;
            $returnData['msg'] = '举报成功！';
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = '举报失败！';
            return json_encode($returnData);
        }
    }
    /*====举报End=====*/

    /*====反馈建议Start=====*/
    /**
     * feedback 反馈建议
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回状态
     */
    public function feedback(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = [
            'feedback_user_id'=>$user_id,
            'feedback_status'=>$status,
            'feedback_content'=>$content
        ];
        $res = Db::table('up_feedback')
            ->insert($data);
        if($res){
            $returnData['status'] = 200;
            $returnData['msg'] = '反馈成功！';
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = '反馈失败！';
            return json_encode($returnData);
        }
    }
    /*====反馈建议End=====*/

    /*====实名认证End=====*/
    /**
     * certification 实名认证
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回状态
     */
    public function certification(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = [
            'user_facade_img'=>$facade_img,
            'user_ldentity_img'=>$ldentity_img,
            'user_hand_img'=>$hand_img,
            'user_card'=>$card,
            'user_rel_name'=>$rel_name,
            'user_attestation'=>3
        ];
        $res = Db::table('up_u_user')
                ->where('user_id',$user_id)
                ->update($data);
        if($res){
            //发送短信
            $phone = Db::table('up_u_user')
                ->where('user_id', $user_id)
                ->value('user_phone');
            $num = 2;
            $this->duaxin($phone,$num);
            $this->Jpush("$user_id",'尊敬的用户，您的实名认证信息已提交成功，我们会尽快完成审核，感谢您使用本平台服务！',array('hello'=>'ioioiioi'));
            //==end==
            $returnData['status'] = 200;
            $returnData['msg'] = '实名认证成功！';
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = '实名认证失败！';
            return json_encode($returnData);
        }
    }
    /*====实名认证End=====*/

    /*====我的资料Start=====*/
    /**
     * myProfile 获取我的资料
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回状态
     */
    public function myProfile(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_u_user')
            ->alias('uu')
            ->join('up_area u','uu.user_province=u.district_id','left')
            ->join('up_area ua','uu.user_city=ua.district_id','left')
            ->join('up_area uaa','uu.user_area=uaa.district_id','left')
            ->where('user_id',$user_id)
            ->field('uu.*,u.district shen,ua.district shi,uaa.district qu')
            ->find();
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * updateMyProfile 修改我的资料
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回状态
     */
    public function updateMyProfile(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = array();
        if(isset($user_image)){
            $data['user_image'] = $user_image;
        }
        if(isset($user_username)){
            $data['user_username'] = $user_username;
        }
        if(isset($user_gender)){
            $data['user_gender'] = $user_gender;
        }
        if(isset($user_province)){
            $data['user_province'] = $user_province;
        }
        if(isset($user_city)){
            $data['user_city'] = $user_city;
        }
        if(isset($user_area)){
            $data['user_area'] = $user_area;
        }
        if(isset($user_signature)){
            $data['user_signature'] = $user_signature;
        }
        $res = Db::table('up_u_user')
            ->where('user_id',$user_id)
            ->update($data);
        if($res){
            $returnData['status'] = 200;
            $returnData['msg'] = '资料修改成功！';
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = '资料修改成功！';
            return json_encode($returnData);
        }
    }
    /*====我的资料End=====*/

    /*====我的寻狗Start=====*/
    /**
     * getMySeekDog 展示我为发布的和我发布的
     * $preserve  为1未发布，为2已发布
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数组
     */
    public function getMySeekDog(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        if($preserve == 1){//未发布
            $condition = 'ud.dog_update_time desc';
        }elseif($preserve == 2){//已发布
            $condition = 'ud.dog_release_time desc';
        }
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_area ua','ud.area_shi=ua.district_id','left')
            ->join('up_area uaa','ud.area_qu=uaa.district_id','left')
            ->join('up_dog_family udf','ud.udf_id=udf.udf_id','left')
            ->join('up_dog_seek_characteristic uds','ud.dog_seek_characteristic=uds.udskc_id','left')
            ->where(['ud.dog_type'=>4,'ud.dog_preserve'=>$preserve,'ud.user_id'=>$user_id])
            ->field('ud.dog_id,ud.dog_img,ua.district shi,uaa.district qu,ud.dog_release_time,udf.udf_name,udf.udf_img,ud.dog_seek_time,ud.dog_add,ud.dog_seek_status,uds.udskc_habitus,uds.udskc_hair,uds.udskc_sex,uds.udskc_tail,uds.udskca_id,uds.udskca_id_other,uds.udskci_id,uds.udskci_id_other')
            ->order($condition)
            ->limit(($page-1)*10,10)
            ->select();
        for($i = 0;$i<count($data);$i++){
            $b = Db::table('up_dog_seek_characteristic_apparel')
                ->where('udskca_id','IN',$data[$i]['udskca_id'])
                ->column('udskca_name');
            $a = Db::table('up_dog_seek_characteristic_other')
                ->where('udsci_id','IN',$data[$i]['udskci_id'])
                ->column('udsci_name');
            $b_num = explode(',',$data[$i]['udskca_id']);
            $a_num = explode(',',$data[$i]['udskci_id']);
            if(in_array(0,$b_num)){
                $b['other'] = $data[$i]['udskca_id_other'];
            }
            if(in_array(0,$a_num)){
                $a['other'] = $data[$i]['udskci_id_other'];
            }

            $data[$i]['apparel'] = $b;
            $data[$i]['others'] = $a;
        }
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * releaseMySeekDog 展示未发布的寻狗详情
     * $dog_id  寻狗动态id
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数组
     */
    public function releaseMySeekDog(){
        extract (input ());
        $auth = $this->checkAll ($user_id , $time , $value , $reqKey);
        if ( $auth == false ) {
            $returnData[ 'status' ] = 301;
            $returnData[ 'msg' ] = "Token失效！请重新登录！";
            return json_encode ($returnData);
        }
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_area u','ud.area_sheng=u.district_id','left')
            ->join('up_area ua','ud.area_shi=ua.district_id','left')
            ->join('up_area uaa','ud.area_qu=uaa.district_id','left')
            ->join('up_dog_family udf','ud.udf_id=udf.udf_id','left')
            ->join('up_dog_seek_characteristic uds','ud.dog_seek_characteristic=uds.udskc_id','left')
            ->where('dog_id',$dog_id)
            ->field('ud.dog_id,ud.area_sheng,ud.area_shi,ud.area_qu,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,u.district sheng,ua.district shi,uaa.district qu,ud.dog_release_time,udf.udf_name,udf.udf_img,ud.dog_seek_time,ud.dog_add,ud.dog_seek_status,uds.udskc_habitus,uds.udskc_hair,uds.udskc_sex,uds.udskc_tail,uds.udskca_id,uds.udskca_id_other,uds.udskci_id,uds.udskc_id,uds.udskci_id_other,ud.dog_seek_people,ud.dog_seek_phone,ud.dog_seek_money_xx,ud.dog_seek_money_ok,ud.dog_information,ud.dog_img,ud.dog_seek_so_qita,ud.dog_seek_so,udf.udf_id')
            ->find();
        if($data['dog_seek_so'] !== 0){
            $data['reason_name'] = Db::table('up_dog_miss_reason')
                ->where('reason_id',$data['dog_seek_so'])
                ->value('reason_name');
        }
        $data['dog_seek_time'] = substr($data['dog_seek_time'],0,-3);
        if($data['dog_img']){
            $data['dog_img'] = trim($data['dog_img'],',');
            $data['dog_img'] = explode(',',$data['dog_img']);
        }
        $b = Db::table('up_dog_seek_characteristic_apparel')
            ->where('udskca_id','IN',$data['udskca_id'])
            ->column('udskca_name');
        $a = Db::table('up_dog_seek_characteristic_other')
            ->where('udsci_id','IN',$data['udskci_id'])
            ->column('udsci_name');
        $b_num = explode(',',$data['udskca_id']);
        $a_num = explode(',',$data['udskci_id']);
        if(in_array(0,$b_num)){
            $b['other'] = $data['udskca_id_other'];
        }
        if(in_array(0,$a_num)){
            $a['other'] = $data['udskci_id_other'];
        }
        $data['apparel'] = implode($b,' ');
        $data['others'] = implode($a,' ');
        if($data['udskc_sex'] == 1){$data['udskc_sex_show'] = '公';}else if($data['udskc_sex'] == 2){$data['udskc_sex_show'] = '母';}else{$data['udskc_sex_show'] ="";}
        if($data['udskc_hair'] == 1){$data['udskc_hair_show'] = '长毛';}else if($data['udskc_hair'] == 2){$data['udskc_hair_show'] = '短毛';}else{$data['udskc_hair_show'] = '';}
        if($data['udskc_habitus'] == 1){$data['udskc_habitus_show'] = '小型狗';
        } else if($data['udskc_habitus'] == 2){
            $data['udskc_habitus_show'] = '中型狗';
        }elseif($data['udskc_habitus'] == 3){
            $data['udskc_habitus_show'] = '大型狗';
        }else{
            $data['udskc_habitus_show'] = '';
        }
        if($data['udskc_tail'] == 1){
            $data['udskc_tail_show'] = '长尾';
        } else if($data['udskc_tail'] == 2){
            $data['udskc_tail_show'] = '短尾';
        }elseif($data['udskc_tail'] == 3){
            $data['udskc_tail_show'] = '无尾';
        }else{
            $data['udskc_tail_show'] = '';
        }

        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * releaseConfirm 确认发布未发布的寻狗动态
     * $dog_id  寻狗动态id
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数组
     */
    public function releaseConfirm(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        // 启动事务
        Db::startTrans();
        try{
            /*前台收集到的表单数据 寻狗特征*/
            $arr = [
                'udskc_sex'=>$udskc_sex,
                'udskc_habitus'=>$udskc_habitus,
                'udskc_hair'=>$udskc_hair,
                'udskc_tail'=>$udskc_tail,
                'udskca_id'=>$udskca_id,
                'udskci_id'=>$udskci_id,
                'udskca_id_other'=>$udskca_id_other,
                'udskci_id_other'=>$udskci_id_other
            ];
			
            Db::table('up_dog_seek_characteristic')->where('udskc_id',$udskc_id)->update($arr);
			//return json_encode(['status'=>200,'msg'=>'11']);
            /*前台收集到的表单数据 发布寻狗*/
            $data = [
                'udf_id'=>$udf_id,
                'area_sheng'=>$sheng,
                'area_shi'=>$shi,
                'area_qu'=>$qu,
                'dog_add'=>$add,
                'dog_information'=>$dog_information,
                'dog_seek_time'=>$seek_time,
                'dog_seek_so'=>$seek_so,
                'dog_seek_people'=>$seek_people,
                'dog_seek_phone'=>$seek_phone,
                'dog_seek_money_xx'=>$seek_money_xx,
                'dog_seek_money_ok'=>$seek_money_ok,
                'dog_lon'=>$lon,
                'dog_lat'=>$lat,
                'dog_seek_so_qita'=>$seek_so_qita,
                'dog_img'=>$dog_img,
                'dog_seek_date'=>substr($seek_time,0,10),
                'dog_year'=>date('Y'),
                'dog_month'=>date('Y-m'),
                'dog_time' => date('Y-m-d')
            ];
            if($dog_img ==""){
                if($type == 1){
                    $img = Db::table('up_dog_family')
                        ->where('udf_id',$udf_id)
                        ->value('udf_img');
                    if($img){
                        $data['dog_img'] = trim($img,',');
                    }
                }
            }
            $time = date('Y-m-d H:i:s');
            if($type == 1){
                $data['dog_preserve'] = 2;
                $data['dog_release_time'] = $time;
                $data['dog_time'] = date('Y-m-d');
                $data['dog_update_time'] = $time;

                Db::table('up_dog')
                    ->where('dog_id',$dog_id)
                    ->update($data);
                $res = Db::table('up_integral')
                    ->where(['integral_status'=>3,'integral_user_id'=>$user_id])
                    ->whereTime('integral_time', 'today')
                    ->count();
                if($res<2){
                    Db::table('up_integral')
                        ->insert(['integral_status'=>3,'integral_user_id'=>$user_id,'integral_time'=>date('Y-m-d H:i:s'),'integral_num'=>5,'integral_dog_id'=>$dog_id]);
                    Db::table('up_u_user')
                        ->where('user_id',$user_id)
                        ->setInc('user_integral',5);
                }
            }else if($type == 2){
                $data['dog_preserve'] = 1;
                $data['dog_update_time'] = $time;
                Db::table('up_dog')
                    ->where('dog_id',$dog_id)
                    ->update($data);
            }
            // 提交事务
            Db::commit();
            $returnData['status'] = 200;
            $returnData['msg'] = "发布成功！";
            return json_encode($returnData);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $returnData['status'] = 0;
            $returnData['msg'] = "发布失败！";
            return json_encode($returnData);
        }
    }
    /*====我的寻狗End=====*/

    /*====自动定位Start=====*/
    /**
     * location 自动定位返回id
     * $location  地区名称
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数组
     */
    public function location (){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        if(isset($sheng)){
            $shengID = Db::table('up_area')
                ->where('district',$sheng)
                ->value('district_id');
            $res['sheng'] = $shengID;
        }
        if(isset($shi)){
            $shiID = Db::table('up_area')
                ->where('district',$shi)
                ->value('district_id');
            $res['shi'] = $shiID;
        }
        if(isset($qu)){
            $quID = Db::table('up_area')
                ->where('district',$qu)
                ->value('district_id');
            $res['qu'] = $quID;
        }
        return json_encode(['status'=>200,'msg'=>$res]);
    }
    /*====自动定位End=====*/

    /*====积分明细Start=====*/
    /**
     * getIntegralData  获取用户积分明细
     * $page  分页
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数组
     */
    public function getIntegralData(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        if($page>=10){
            $page = 10;
        }
        $data = Db::table('up_integral')
            ->where(['integral_user_id'=>$user_id])
            ->field('integral_status,integral_time,integral_num')
            ->order('integral_time desc')
            ->limit(($page-1)*10,10)
            ->select();
        for($i=0;$i<count($data);$i++){
            switch ($data[$i]['integral_status'])
            {
                case 1:
                    $data[$i]['integral_status'] = "注册";
                    break;
                case 2:
                    $data[$i]['integral_status'] = "实名认证";
                    break;
                case 3:
                    $data[$i]['integral_status'] = "发布内容";
                    break;
                case 4:
                    $data[$i]['integral_status'] = "留言";
                    break;
                default:
                    $data[$i]['integral_status'] = "领养成功";
            }
        }
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /*====积分明细End=====*/
    /*====广告图Start=====*/
    /**
     * getAdvertisingData 获取广告图
     * $type  1：流浪狗   2：互助    3：领养    4：寻狗
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数组
     */
    public function getAdvertisingData(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_advertising')
            ->where(['ad_type'=>$type,'ad_status'=>1])
            ->field('ad_img')
            ->find();
        if($data == null){
            $data = "";
        }
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /*====广告图End=====*/

    /*====进展点赞Start=====*/
    /**
     * likeThis 进展点赞
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 状态
     */
    public function likeThis(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $where = [
            'ju_user_id'=>$user_id,
            'ju_j_id'=>$j_id
        ];
        $have = Db::table('up_jinzhan_user')->where($where)->find();
        if($have){
            Db::startTrans();
            try{
                Db::table('up_jinzhan_user')
                    ->where($where)
                    ->delete();
                Db::table('up_jinzhan')->where('j_id', $j_id)->setDec('j_zan');
                // 提交事务
                Db::commit();
                $returnData['status'] = 200;
                $returnData['msg'] = "取消点赞成功！";
                return json_encode($returnData);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $returnData['status'] = 0;
                $returnData['msg'] = "取消点赞失败！";
                return json_encode($returnData);
            }
        }else{
            Db::startTrans();
            try{
                Db::table('up_jinzhan_user')
                    ->insert($where);
                Db::table('up_jinzhan')
                    ->where('j_id', $j_id)
                    ->setInc('j_zan');
                // 提交事务
                Db::commit();
                $returnData['status'] = 200;
                $returnData['msg'] = "点赞成功！";
                return json_encode($returnData);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $returnData['status'] = 0;
                $returnData['msg'] = "点赞失败！";
                return json_encode($returnData);
            }
        }
    }
    /*====进展点赞End=====*/


}